
//These are scripts that will not have an effect unless they are run AFTER the subpage is fully loaded into the dom (because they perform an action on a specific item in the subpage). The function onLoadFunctions has to be inserted into ondomready: line in the index.html.
function onLoadFunctions() {
//if the Id element myPic1 exists, set it's source to the localStorage value for pic1Icon and the source for myPic2 to pic2Icon- this is used to set an image using localStorage.
if (document.getElementById('myPic1') != null) {
document.getElementById('myPic1').src = localStorage.getItem('pic1Icon');
document.getElementById('myPic2').src = localStorage.getItem('pic2Icon');
document.getElementById('myPic3').src = localStorage.getItem('pic3Icon');}

//if the Id element deldiv exists, check to see if the localStorage key delStorage exists. The localStorage key is used to determine what message is shown.
if (document.getElementById('deldiv') != null) {
	if (localStorage.getItem('delStorage') == null) {
		document.getElementById('deldiv').innerHTML = 'delStorage is not set.';}
	else if (localStorage.getItem('delStorage') != null) {
		document.getElementById("deldiv").innerHTML = localStorage.getItem('delStorage');} 
	}
		
//if the Id element myFrame exists, set it's source to the localStorage value for iframesrc - this is used to set an iframe source using localStorage.
if (document.getElementById('myFrame') != null) {
document.getElementById("myFrame").src = localStorage.getItem('iframesrc');}

//if the Id element secretqdiv exists, set it's source to the localStorage value for secretq - this is used for the Forgot Password screen.
if (document.getElementById('secretqdiv') != null) {
document.getElementById("secretqdiv").innerHTML = localStorage.getItem('secretq');}

//Hide the Background Info section. This requires two parts: first, check for the localStorage item. Second, make sure the element exists. If you try to run a function on an element that doesn't exist, any additional JavaScript in that function will not run.
if (localStorage.getItem('hideinfo') != null) { 
	if (document.getElementById('hideinfo') != null) {document.getElementById('hideinfo').style.display = 'none'}
}


//These functions allow the Score Keeping Names to show up, and show the scores when you load the scores page.
if (document.getElementById('showStorage1') != null) {
	document.getElementById("showStorage1").innerHTML = localStorage.getItem('player1');
	document.getElementById("showStorage2").innerHTML = localStorage.getItem('player2');
	document.getElementById("showStorage3").innerHTML = localStorage.getItem('player3');
	document.getElementById("showStorage4").innerHTML = localStorage.getItem('player4');
	document.getElementById("player1title").innerHTML = localStorage.getItem('player1name');
	document.getElementById("player2title").innerHTML = localStorage.getItem('player2name');
	document.getElementById("player3title").innerHTML = localStorage.getItem('player3name');
	document.getElementById("player4title").innerHTML = localStorage.getItem('player4name');}
if (document.getElementById('player1box') != null) {
	document.getElementById('player1box').value = localStorage.getItem('player1name');
	document.getElementById('player2box').value = localStorage.getItem('player2name');
	document.getElementById('player3box').value = localStorage.getItem('player3name');
	document.getElementById('player4box').value = localStorage.getItem('player4name');}
if (localStorage.getItem('player1') == null) {localStorage.setItem('player1','0');}
if (localStorage.getItem('player2') == null) {localStorage.setItem('player2','0');}
if (localStorage.getItem('player3') == null) {localStorage.setItem('player3','0');}
if (localStorage.getItem('player4') == null) {localStorage.setItem('player4','0');}
if (localStorage.getItem('player1name') == null) {localStorage.setItem('player1name','Player 1');}
if (localStorage.getItem('player2name') == null) {localStorage.setItem('player2name','Player 2');}
if (localStorage.getItem('player3name') == null) {localStorage.setItem('player3name','Player 3');}
if (localStorage.getItem('player4name') == null) {localStorage.setItem('player4name','Player 4');}

//This is the code needed to have a counter.
if (localStorage.getItem('counter') == null) {localStorage.setItem('counter','0');}
if (document.getElementById('counter') != null) {document.getElementById("counter").innerHTML = localStorage.getItem('counter');}

//Lastly, restore the scroller.
bb.scroller.refresh()
}

//the Welcome function - determines whether the welcome tutorial or the main app is opened.
function welcome() {
if (localStorage.getItem('welcome') === null) {
 bb.pushScreen('welcome.html', 'welcome');}
else
bb.pushScreen('menu.html', 'menu');
}

//the function for the redirect option, allowing a single button to redirect to different pages depending on what is set in localStorage.
function redirect() {
if (localStorage.getItem('redirect') === null) {
 bb.pushScreen('redirect/one.html', 'one');}
else if (localStorage.getItem('redirect') === 'something') {
 bb.pushScreen('redirect/three.html', 'three');}
else
bb.pushScreen('redirect/two.html', 'two');
}

//functions for entering a password, setting a password, redirecting or denying access to the password setup screen, setting the secret question and answer, and reseting the password
function passprotect() {
if (localStorage.getItem('password') === null) {
alert('No password set. You will be redirected to options to set one up.');
bb.pushScreen('password/setpassword.html','setmypass');}
else if (localStorage.getItem('password') == mypass.value) {bb.pushScreen('password/success.html','success')}
else {alert('You entered the wrong password. Please try again.')}
}
function setpass() {
if (setpass1.value == setpass2.value) {
localStorage.setItem('password',setpass1.value)}
}
function setpassscreen() {
if (localStorage.getItem('password') === null) {bb.pushScreen('password/setpassword.html','setmypass')}
else {alert('You have already set your Password.');}
}
function setsecret() {
localStorage.setItem('secretq',secretquestion.value);
localStorage.setItem('secreta',secretanswer.value);
}
function resetpass() {
if (localStorage.getItem('secreta') == secretanswer2.value) {
localStorage.removeItem('password');alert('Password Reset');}
else {alert('Wrong Secret Answer');}
}

//This is the function for saving a JSON string into localStorage.
function saveJSON() {
localStorage.setItem('jsonString', JSON.stringify({
	option1: 'Hello World',
	option2: 'Goodbye World',
	option3: document.getElementById('JSONbox').value,
	}))
}

//About Section functions and launchers
function launchGit() { 
var remote = new blackberry.transport.RemoteFunctionCall("blackberry/invoke/invoke");
remote.addParam("appType", "https://github.com/blackberry/bbUI.js");
remote.makeAsyncCall();
}
function launchOSBB() { 
var remote = new blackberry.transport.RemoteFunctionCall("blackberry/invoke/invoke");
remote.addParam("appType", "http://www.opensourcebb.com");
remote.makeAsyncCall();
}
function launchOSBBx() { 
var remote = new blackberry.transport.RemoteFunctionCall("blackberry/invoke/invoke");
remote.addParam("appType", "http://x.opensourcebb.com");
remote.makeAsyncCall();
}
function appWorld() {	
var remote = new blackberry.transport.RemoteFunctionCall("blackberry/invoke/invoke");
remote.addParam("appType", "appworld://vendor/4735");
remote.makeAsyncCall();	
}
function launchCompose() { 
var remote = new blackberry.transport.RemoteFunctionCall("blackberry/invoke/invoke");
remote.addParam("appType", "mailto:Support@SCrApps.org");
remote.makeAsyncCall();
}

//New Defined Function

//common Function

//set html id content
function setContent(id, content) {
    var ele = document.getElementById(id);
    if (ele) {
        ele.innerHTML = content;
    }
}

//
function appendNewDataTest (){
    
    localStorage.setItem('jName', JSON.stringify([{
        numeric1: '44',
        numeric2: '2',
        numeric3: '3',
        numeric4: '4'
        },
    {
        numeric1: '134324',
        numeric2: '342',
        numeric3: '4324',
        numeric4: '23432'
        }
    ]      
    ));
    
    //set local storage from input user (new local storage)
    var jData = [{numeric1: '5', numeric2: '6', numeric3: '7', numeric4: '8'}];
    localStorage.setItem('jName2', JSON.stringify(jData));
    
    //get object from local json storage
    var json1Object = JSON.parse(localStorage.getItem('jName'));
    var json2Object = JSON.parse(localStorage.getItem('jName2'));
    
    //get String from local json storage
    var json1String = JSON.stringify(localStorage.getItem('jName'));
    var json2String = JSON.stringify(localStorage.getItem('jName2'));

    // access object from json
    alert(json1Object[1].numeric1);
    
    var jsonArray1 = [{'name': "doug", 'id':5}, {'name': "dofug", 'id':23}];
    var jsonArray2 = [{'name': "goud", 'id':1}, {'name': "doaaug", 'id':52}];
    jsonArray1 = jsonArray1.concat(jsonArray2);
    alert (typeof jsonArray1);
    
    // concate new json object
    json1Object = json1Object.concat(json2Object);
    alert('type of jSon 1 Object: '+typeof json1Object);
    
    
    localStorage.setItem('jName3', JSON.stringify(json1Object));
    
    alert('append');
}

//append jsonObject2 to last element of jsonObject1
function appendNewData (jsonObject1, jsonObject2){
    alert('call appendNewData( obj1, obj2)');
    //get object from local json storage
    json1 = JSON.parse(JSON.stringify(jsonObject1));
    json2 = JSON.parse(JSON.stringify(jsonObject2));
    alert('type of jSon1: '+typeof json1);
    
    // concate new json object
    json1 = json1.concat(json2);
    alert(' after concat type of jSon1 Object: '+typeof json1);
    
    alert(sessionStorage.getItem('tempJName'));
    localStorage.setItem(sessionStorage.getItem('tempJName'), JSON.stringify(json1));
    alert('append');
}

//sample function to add new data
function addNewData(){
    alert('call addNewData()');
    jName = JSON.parse(localStorage.getItem('jName2'));
    jName2 ={'numeric1': "goud", 'numeric2':"1", 'numeric3': "7", "numeric4": "8"};
    
    //store temporary primary jName 
    sessionStorage.setItem('tempJName', 'jName2');
    alert(sessionStorage.getItem('tempJName'));

    appendNewData(jName, jName2);
    alert('appendNewData');
}

//functions for entering a password, setting a password, redirecting or denying access to the password setup screen, setting the secret question and answer, and reseting the password
function setPass() {
    if (mypass.value != null) {
	localStorage.setItem('password',mypass.value);
        alert (localStorage.getItem('password'));
    }
}
function changePassSetting() {
    if (passSet.value != null) {
        localStorage.setItem('passSet',passSet.value);
        alert (localStorage.getItem('passSet'));
    }
}
function checkPassSetting(){
    if(localStorage.getItem('passSet') == 'yes'){
        alert ('passSetting == yes');
        alert('Insert set password form');
    }
    else {
        alert ('passSetting unset');
        alert ('passSetting =='+localStorage.getItem('passSet'));
    }
}

//Setting Page
function storeDefaultValueTerm(){
    localStorage.setItem('jSettingTerm', JSON.stringify([{
        numeric1: '1',
        numeric2: '2',
        numeric3: '3',
        numeric4: '4',
        seasonal1: 'Spring',
        seasonal2: 'Summer',
        seasonal3: 'Autumn',
        seasonal4: 'Winter'
        }]
    )
    );
    alert('set default value term');    
    //alert (JSON.parse(localStorage.getItem('jSettingTerm')).numeric1);
}
function storeDefaultValueGrade(){
    localStorage.setItem('jGrade', JSON.stringify([{
        'A+':'4.3',
        A: '4',
        'A-':'3.7',
        'B+': '3.5',
        B: '3',
        'B-':'2.7',
        'C+': '2.5',
        C: '2',
        D: '1',
        E: '0',
        F: null
        }])
    )
    alert('set default value grade');
}

//show all grade
function showAllGrade(){
    var out, i, key, value;
    out = "<table width='900px' cellpadding='4px' border='1px'>";
    out += "<tr><td>Key</td><td>Value</td></tr>";
    key = 'jGrade';
    value = window.localStorage.getItem(key);
    out += "<tr><td>" + key + "</td><td>" + value + "</td></td></tr>";
    out += "</table>";
    setContent("grade", out);
}

//
function showAllTerm(){
    var out, i, key, value;
    out = "<table width='900px' cellpadding='4px' border='1px'>";
    out += "<tr><td>Key</td><td>Value</td></tr>";
    key = 'jSettingTerm';
    value = window.localStorage.getItem(key);
    out += "<tr><td>" + key + "</td><td>" + value + "</td></td></tr>";
    out += "</table>";
    setContent("term", out);
}
   
    //numeric3: document.getElementById('JSONbox').value,
//accessing key properties
function jsonConcat(o1, o2) {
    for (var key in o2) {
    o1[key] = o2[key];
    }
    return o1;
}
